<template>
  <div
    style="border:1px solid #C0C0C0; border-radius: 4px; width: 500px; height: 500px;margin: 0 auto"
  >
    <h1>注册</h1>

    <el-form :model="storekeeper" :rules="rules" ref="storekeeper" label-width="180px">
      <el-form-item label="商家名称" prop="storekeeperName">
        <el-input v-model="storekeeper.storekeeperName"></el-input>
      </el-form-item>

      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input v-model="storekeeper.phoneNumber"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="storekeeper.password"></el-input>
      </el-form-item>

      <el-form-item label="身份证号码" prop="identityNumber">
        <el-input v-model="storekeeper.identityNumber"></el-input>
      </el-form-item>

      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="storekeeper.email"></el-input>
      </el-form-item>

      <el-button type="primary" plain v-on:click="change()">返回登录页面</el-button>
      <el-button v-on:click="resetForm('storekeeper')">重置</el-button>
      <el-button type="primary" v-on:click="submitForm('storekeeper')">注册</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      storekeeper: {
        storekeeperName: "",
        phoneNumber: "",
        password: "",
        identityNumber:"",
        email: "",
      },
      rules: {
        storekeeperName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { max: 20, message: "用户名过长", trigger: "blur" },
        ],
        phoneNumber: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { pattern: /^[0-9]+$/, message: "只能输入数字" },
          { min: 11, max: 11, message: "长度错误", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { pattern: /^(\S)*$/, message: "不能输入空格" },
        ],
        identityNumber:[
           { required: true, message: "请输入18位身份证号码", trigger: "blur" },
          { min: 18,max: 18, message: "长度错误" },
        ],
        email: [
          { pattern: /^(\w)+@(\S)+$/, message: "非法邮箱", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    change() {
      // this.$emit("toLogin");
      this.$router.push('login');
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");

          this.$http
            .post(process.env.URL_HEAD+"/store/storekeeper/register", this.storekeeper,{withCredentials:true})
            .then(
              (response) => {
                console.log(response);

                if (response.data.status == "error") {
                  this.$alert("注册失败" + response.data.message, "", {
                    confirmButtonText: "确定",
                  });
                } else if (response.data.status == "ok") {
                  this.$alert("注册成功！", "", {
                    confirmButtonText: "确定",
                  });

                  this.change();
                }
              },

              (response) => {
                this.$alert("系统异常，请重试", "", {
                  confirmButtonText: "确定",
                });
              }
            );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scope>
.h1 {
  font-size: 50px;
}
</style>
